package com._520it.crm.service.impl;

import com._520it.crm.domain.Employee;
import com._520it.crm.domain.InsuranceInfoChange;
import com._520it.crm.domain.OrderBill;
import com._520it.crm.mapper.InsuranceInfoChangeMapper;
import com._520it.crm.mapper.OrderBillMapper;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.InsuranceInfoChangeQueryObject;
import com._520it.crm.service.IInsuranceInfoChangeService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by ASUS on 2017/10/9.
 */
@Service
public class InsuranceInfoServiceImpl implements IInsuranceInfoChangeService{
    @Autowired
    private InsuranceInfoChangeMapper insuranceInfoChangeMapper;
    @Autowired
    private OrderBillMapper orderBillMapper;


    @Override
    public int deleteByPrimaryKey(Long id) {
        return insuranceInfoChangeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(InsuranceInfoChange insuranceInfoChange) {
        return insuranceInfoChangeMapper.insert(insuranceInfoChange);
    }

    @Override
    public InsuranceInfoChange selectByPrimaryKey(Long id) {
        return insuranceInfoChangeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<InsuranceInfoChange> selectAll() {
        return insuranceInfoChangeMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(InsuranceInfoChange insuranceInfoChange) {
        return insuranceInfoChangeMapper.updateByPrimaryKey(insuranceInfoChange);
    }

    public PageResult query(InsuranceInfoChangeQueryObject qo) {
        Integer totalCount = insuranceInfoChangeMapper.queryForCount(qo);
        if (totalCount==0){
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<InsuranceInfoChange> data = insuranceInfoChangeMapper.queryForList(qo);
        return new PageResult(totalCount,data);
    }

    @Override
    public InsuranceInfoChange approvalByInsurancePolicyNumber(String insurancePolicyNumber) {
        try {
            InsuranceInfoChange insuranceInfoChange = insuranceInfoChangeMapper.approvalByInsurancePolicyNumber(insurancePolicyNumber);
            return insuranceInfoChange;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void insertMutiplate(InsuranceInfoChange insuranceInfoChange) {
        try {
            Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
            insuranceInfoChangeMapper.setInsuranceInfo(employee.getSn(),new Date(),insuranceInfoChange.getApprovalassign(),insuranceInfoChange.getId());
            insuranceInfoChange.setChecknumber("0003123"+new Date().getTime()+"");
            Long id = insuranceInfoChange.getOrderbill().getId();
            if (id!=null){
                orderBillMapper.tempSave(OrderBill.STATE_ZC,id,employee.getSn());
            }
            insuranceInfoChangeMapper.insertMutiplate(insuranceInfoChange);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
